﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class enterprise : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        

        string name_tocheck1 = this.Request.Params["q"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        if (check_field(name_tocheck1) == true)//查询的是行业名称
        {
            //在INFO_ENTER中查询这个行业的所有企业，返回的信息包括：企业名称，企业规模，企业行业，联系电话，业务地区
            
            SqlCommand com_field = new SqlCommand();
            com_field.Connection = sc;
            com_field.CommandText = "select ENTER_NAME,ENTER_SIZE,ENTER_FIELD,ENTER_PHONE,ENTER_DISTRICT from INFO_ENTER where ENTER_FIELD=@field";
            com_field.Parameters.AddWithValue("@field",name_tocheck1);
            SqlDataReader reader = com_field.ExecuteReader();
            GridView1.DataSource = reader;
            GridView1.DataBind();
        }
        else//查询的是完整企业名称
        {
            //先判断企业是否存在，若不存在返回：企业信息不完整或不存在
            SqlCommand com_check = new SqlCommand();
            com_check.Connection = sc;
            com_check.CommandText = "select ENTER_ID from INFO_ENTER where ENTER_NAME=@name";
            com_check.Parameters.AddWithValue("@name", name_tocheck1);
            if (com_check.ExecuteScalar()== null)
            {
                GridView1.Visible = false;
                Label1.Text = "企业信息不完整或不存在。";
            }
            else //在INFO_ENTER中查询指定企业的信息，返回数据的如上
            {
                
                SqlCommand com2 = new SqlCommand();
                com2.Connection = sc;
                com2.CommandText = "select ENTER_NAME,ENTER_SIZE,ENTER_FIELD,ENTER_PHONE,ENTER_DISTRICT from INFO_ENTER where ENTER_NAME=@name";
                com2.Parameters.AddWithValue("@name",name_tocheck1);
                SqlDataReader reader2 = com2.ExecuteReader();
                GridView1.DataSource = reader2;
                GridView1.DataBind();
            }
           
        }
        
    }
    public bool check_field(string input)
    {
        if (input.Equals("餐饮") == true || input.Equals("教育") == true || input.Equals("交通") == true || input.Equals("IT") == true || input.Equals("娱乐") == true || input.Equals("生活") == true)
            return true;
        else return false;
    }
}